import { Inject, Controller, Get, Query } from "@midwayjs/core";
import { NlpPkgService } from "../service/nlp.pkg.service";
import { ModelService } from "../service/model.service";
import { BaseController } from "./base.controller";

@Controller("/api")
export class APIController extends BaseController {
  // @Inject()
  // ctx: Context;
  @Inject()
  nlpPkgService: NlpPkgService;
  @Inject()
  modelService: ModelService;

  @Get("/npl")
  async getUser(@Query() queryData) {
    const text = queryData.text;
    try {
      // 获取自然语言处理结果
      const res = await this.nlpPkgService.getNlpResult(text);
      console.log("nlpPkgService.getNlpResult:", res);
      let resultTemplate = null;
      // 不一定有分词结果
      if ("word" in res) {
        resultTemplate = this.modelService.matchModel(<AnalysisResult>res, [
          "actionKeywords",
          "targetKeywords",
        ]);
      }
      if (resultTemplate) {
        this.success(resultTemplate);
      } else {
        this.error("No model found");
      }
    } catch (err) {
      console.log(err);
      this.error("No model found");
    }

    // return { success: true, message: "OK", data: text };
  }
}
